var Set = {
	$event: null,
	init: function() {
		$("#name").text(CurrentUser.name);
		$("#phone,#pphone").text(CurrentUser.phone);
		$("#photo,#picture").css("background-image", "url('"+ Core.id2url(CurrentUser) +"')");
	},
	editFiled: function(title) {
		SetLS("set_edit_filed", {title:title});
		OpenNewWin("set_edit");
	},
	edit: function(){
	    var name =CurrentUser.category==30?"修改名称":"Editname";
	    if (filed.title == name) {
	        if(!$("#cname").val()) {
	          return (CurrentUser.category==30?$alert("请输入您要修改的新名字！"):$alertEn("Please input your new name"));  
	        }
	        var data = CurrentUser;
	        data.name = $("#cname").val();
	        $ajax({
	            url: "/user/upsert",
	            data: data,
	            success: function(res) {
	            	CurrentUser.name = data.name;
	            	SetLS("user", CurrentUser);
	            	var lanPath = lan==0?"ch/":"en/";	
	            	PCall("set","$('#name').text('" + data.name + "')");
	            	PCall(lanPath+"index","$('#name').text('" + data.name + "')");
	            	PCall("set", "CCall('Set.init();')");
	                WinClose();
	            }
	        });
	    } else {
	    	var opwm = $("#oldpwd").val();
	        if(!opwm)
	        	return (CurrentUser.category==30?$alert("请输入旧密码！"):$alertEn("Old password!"));
			if (!$("#pwd").val() || !$("#cfmPwd").val())
				return (CurrentUser.category==30?$alert("请填写完整！"):$alertEn("Please type completely!"));
			if ($("#pwd").val() != $("#cfmPwd").val()){
				$("#pwd").val("");
				$("#cfmPwd").val("");
				return (CurrentUser.category==30?$alert("两次输入的密码不同，请重新输入!"):$alertEn("Two times the password is different, please re-enter!"));
			}
			var data = CurrentUser;
			data.oldPassword = opwm;
			data.password = $("#pwd").val();
			$ajax({
				url: "/user/updatePassword",
				data: data,
				success: function(res){
					(CurrentUser.category==30?$alert("密码修改成功"):$alertEn("Changed password successfully"));
	                WinClose();
				}
			});
	    }
	},
	mphoto: function() {
		Upload.init(Core.API_VER + "/user/updatephoto?id="+CurrentUser._id, function(res) {
			CurrentUser.photo = res[0].result.url;
			SetLS("user", CurrentUser);
			var photo = Core.id2url(CurrentUser);
			$("#photo").css("background-image", "url('" + photo + "')");
			var lanPath = lan==0?"ch/":"en/";			
			PCall(lanPath+"index","$('#photo').css('background-image','url(" + photo + ")')");
		});
	},
	lgnout: function() {
		ActionSheet(CurrentUser.category==30?"你确定退出登录吗？":"Are you sure to logout?", CurrentUser.category==30?"取消":"Cancel", new Array(CurrentUser.category==30?"退出登录":"Logout"), function() {
			SetLS("user", null);
			CurrentUser = "";

			var lanPath = lan==0?"ch/":"en/";
			PCall("ch/index","WinClose();");
			PCall("en/index","WinClose();");
			//PCall(lanPath+"index","WinClose();");
			WinClose();
			OpenNewWin("../signup");
		});
	},
	birthdayDatePicker: function(data){
		var date = data.year+"-"+data.month+"-"+data.day;
		$("#birthday").val(date);
	},
	uploadPhoto: function(){
		Upload.init(Core.API_VER + "/upload", function(res) {
			res = res[0];
		 	if (res.code) {
		 		$alert(res.codemsg);
		 		return;
		 	}                
            var url = res.result.url; 
            $("#passportParent").html('<div id="passport" data-photo="' + url + '" class="uw70 umauto photo"><img src="' + Core.IMG_VER + url + '" class="uw100"></div>');
		});
	},
	resumeInit: function(){
		$ajax({
			url : "/user/detail",
			data: {
				_id: CurrentUser._id
			},
			success: function(res){
				var data = res.result;
				var category=data.category;
				SetLS("Usercategory",category);
				$(".value").each(function(i){
					if ($(this).attr("id") == "birthday"){
					   if(!IsDefine(data.birthday)){
					       $(this).val("");
					   }else{
					       $(this).val((new Date(data.birthday)).pattern("yyyy-MM-dd"));
					   }
					   
					}else{
						$(this).val(data[$(this).attr("id")]);
					}
				});
				var sex=data.sex;
                    if(sex == "男"||sex =="male"){
                       $("#checkboy").addClass("im_checkbox_selected");  
                    }else if(sex == "女"||sex =="female"){
                       $("#checkgirl").addClass("im_checkbox_selected")
                    }
                var cat=data.category;
                    if(cat == 30){
                       $("#checkChina").addClass("im_checkbox_selected1");  
                    }else if(cat == 20){
                       $("#checkExpat").addClass("im_checkbox_selected1")
                    }
				//data.sex=$(".im_checkbox").text(data.sex);
				//data.category=$(".im_checkbox1").text(data.category);
				// var experience = '';
				// $.each(data.experience[0].desc,function(i,item){
					// //experience += Set.getExperienceHtml((new Date(item.start)).pattern("yyyy-MM-dd"),(new Date(item.end)).pattern("yyyy-MM-dd"),item.desc);
					// experience += Set.getExperienceHtml("","",item.desc);
				// });
				// $("#experienceList").html(experience);
				data.passport&&$("#passportParent").html('<div id="passport" data-photo="' + data.passport + '" class="uw70 umauto photo"><img src="' + Core.IMG_VER + data.passport + '" class="uw100"></div>');
				$("input[type='radio'][name='sex'][value='" + data.sex + "']").attr("checked","checked");
				if(category==30){
				    $("input[type='radio'][name='person'][value='china']").attr("checked","checked");
				}else{
				    $("input[type='radio'][name='person'][value='expat']").attr("checked","checked");
				}
				var experience=data.experience[0].desc;
				$("#experience").val(experience);
			}
		});
	},
	getExperienceHtml: function(start,end,desc){
	    var exp=CurrentUser.category==30?"工作经历":"Person Property"
		return 	'<div class="ub ub-ver singleExperience">' + 
		        '   <div class="im_icon_faq ub-img uh2 uw2 um_base umt_2">'+
		        '       <div class="uw5 uml_3 umt_p5">'+exp+'</div>'+
		        '   </div>'+
                // '    <div class="ub ub-ac uplr_1">' + 
                // '        <input class="ub ub-f1 uc-a1 input_sz start" type="text" readonly="readonly" value="' + start + '" placeholder='+(CurrentUser.category==30?"开始日期":"Start_Date")+' onclick="Set.experienceDate();">　-　<input class="ub ub-f1 uc-a1 input_sz end" type="text" readonly="readonly" value="' + end + '" placeholder='+(CurrentUser.category==30?"结束日期":"End_Date")+' onclick="Set.experienceDate();">' +     
                // '    </div>' + 
                '    <div class="ub up1 ub-pc uw12 uml_8l umt_5f">' + 
                '        <textarea class="ub ub-f1 experience" placeholder='+(CurrentUser.category==30?"请填写你的工作经历":"Please_input_your_Job_experience")+'>'+ desc + '</textarea>' + 
                '    </div>' + 
                '</div>';
                //
                //(CurrentUser.category==30?"开始日期":"Start Date")
	},
	addExperience: function(){
		$("#experienceList").append(Set.getExperienceHtml("","",""));
	},
	resumeEdit: function(){
		var data = CurrentUser;
		$(".value").each(function(i){
			data[$(this).attr("id")] = $(this).val();
		});
		//data.sex = $("input[type='radio'][name='sex']:checked").val();
		//var cate = $("input[type='radio'][name='person']:checked").val();
		var cate=$(".im_checkbox_selected1").children(":first").text();
		if(cate == "Chinese"||cate == "中国人"){
		    data.category = 30;
		}else if(cate == "Expat"||cate == "外国人"){
		     data.category = 20;
		}
		data.sex=$(".im_checkbox_selected").children(":first").text();
		data.passport = $("#passport").data("url");
		data.experience = new Array();
		$(".singleExperience").each(function(i){
			data.experience[i] = new Object();
			// data.experience[i].start = $(this).find(".start").val();
			// data.experience[i].end = $(this).find(".end").val();
			data.experience[i].desc = $(this).find(".experience").val();
		});
		//return;
		$ajax({
			url: "/user/upsert",
			data: data,
			success: function(res){
			    if(GetLS("Usercategory") != data.category){
			        SetLS("user", null);
                    CurrentUser = "";
                    var lanPath = lan==0?"ch/":"en/";
                    PCall("ch/index","WinClose();");
                    PCall("en/index","WinClose();");
                    WinClose();
                    OpenNewWin("../signup");
			        return;
			    }
				WinClose();
			}
		});
	},
	experienceDate: function(){
		Set.$event = $(event.currentTarget);
		DatePicker('','','',Set.experienceDatePicker);
	},
	experienceDatePicker: function(data){
		var date = data.year+"-"+data.month+"-"+data.day;
		Set.$event.val(date);
	}
}